<?php
/**
 * 用户业务模型类
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Admin_user extends Controller
{
	private $result;

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();
		
		$this->loadModel('user_model');

		$this->tpl->template_dir = SYS_PATH  . 'template/admin/user/';
	}

	/**
	 * 默认执行方法
	 *
	 * @return max
	 */
	function index()
	{
		$this->getUsers();
	}

	/**
	 * 添加用户
	 * @return max
	 */
	function addUser(){
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("user.html");
	}

	/**
	 * 编辑用户
	 * @return max
	 */
	function editUser(){

		$u_id = $_SESSION['SURVEY']['USER']['u_id'];

		$user = $this->user_model->getUserItemById($u_id);

		$this->tpl->assign("user",$user);
		$this->tpl->display("user.html");
	}

	/**
	 * 获取多个用户
	 * @return max
	 */
	function getUsers(){

		$page = max($this->in_var['page'],1);

		$this->result['projects'] = $this->user_model->getUserItems('',$page,6);

		$page_info = $this->user_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);

		$this->tpl->assign("page_info",$page_info);
		$this->tpl->assign("result",$this->result);

		$this->tpl->display("users.html");

	}

	/**
	 * 修改密码
	 * @return max
	 */
	function changePwd(){

		$method = $this->in_var['request_method'];

		if($method=="post")
		{
			$old = $this->in_var['old'];
			$new = $this->in_var['new'];

			$u_id = $_SESSION['SURVEY']['USER']['u_id'];

			$flag = $this->user_model->changePwd($u_id,$old,$new);

			return exit(json_encode($flag?array('info'=>$this->sys_conf_var['survey']['customer_msg']['user']['changepwd_success'],'status'=>true):$this->result['msg'] = array('info'=>$this->sys_conf_var['survey']['customer_msg']['user']['changepwd_error'],'status'=>false)));

		}
		else
		{
			$this->tpl->display("pwd.html");
		}

	}

	/**
	 * 根据条件查询用户
	 * @return max
	 */
	function searchUsers(){

		$page     = max($this->in_var['page'],1);
		$keywords = $this->in_var['keywords'];

		$this->result['users'] = $this->user_model->getAdsProjectsByKeywords($keywords,$page,6);
		$page_info = $this->user_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);

		$this->tpl->assign("page_info",$page_info);
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("users.html");
	}

	/**
	 * 保存用户信息
	 * @return max
	 */
	function saveUser(){

		$flag = false;
		$user = $this->in_var['user'];

		if(!empty($user['u_id']))
		{
			$flag = $this->user_model->changeUserItemById($user);
		}
		else
		{
			$flag = $this->user_model->addUserItem($user);
		}

		exit(json_encode($flag?array('info'=>$this->sys_conf_var['survey']['customer_msg']['user']['save_success'],'status'=>true):$this->result['msg'] = array('info'=>$this->sys_conf_var['survey']['customer_msg']['user']['save_error'],'status'=>false)));
	}

	/**
	 * 删除多个用户
	 * @return max
	 */
	function delUsers(){

		$ids = $this->in_var['item'];

		$id_ = explode(',',$ids);

		try{

			foreach($id_ as $id)
			{
				$this->user_model->delUserItemById($id);
			}

			exit(json_encode(array('info'=>$this->sys_conf_var['survey']['customer_msg']['user']['del_success'],'status'=>true)));
		}
		catch(Exception $e)
		{
			exit(json_encode(array('info'=>$this->sys_conf_var['survey']['customer_msg']['user']['del_error'],'status'=>false)));
		}
	}

}